package cn.outsourcing.supermarket.common.enums;


import cn.outsourcing.supermarket.common.constants.Channel;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;

@AllArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@Getter
public enum ChannelEnum  {
    /**
     * 支付渠道
     */
    ALIPAY_QUICK("支付宝快捷支付", Channel.ALIPAY_QUICK),
    WECHAT_JSAPI("微信公众号支付", Channel.WECHAT_JSAPI),
    UNION_ONLINE("银联在线支付", Channel.UNION_ONLINE),
    ALIPAY("支付宝即时到账", Channel.ALIPAY),
    UNION_PHONE("银联手机支付", Channel.UNION_PHONE),
    ALIPAY_WAP("支付宝手机网页即时到账", Channel.ALIPAY_WAP),
    ALIPAY_BANK_PAY("支付宝网银支付", Channel.ALIPAY_BANK_PAY),
    UNION_B2B("银联B2B支付", Channel.UNION_B2B),
    OFFLINE_REMIT("线下汇款", Channel.OFFLINE_REMIT),
    BALANCE("余额支付", Channel.BALANCE),
    COUPON("优惠券支付", Channel.COUPON),
    WECHAT_APP("微信APP支付", Channel.WECHAT_APP),
    UNION_TOKEN("银联无跳转支付", Channel.UNION_TOKEN),
    LANJINLING_P2P("金灵贷", Channel.LANJINLING_P2P),
    UNION_GATEWAY("银联网关支付", Channel.UNION_GATEWAY),
    YIBAO("易宝支付", Channel.YIBAO),
    CREDIT_LIMIT("授信宝支付", Channel.CREDIT_LIMIT),
    ALLIN("通联支付", Channel.ALLIN),
    SCF_LOAN_P2P("小巴钱袋", Channel.SCF_LOAN_P2P),
    OFFLINE_SCENE("现场退款", Channel.OFFLINE_SCENE),
    PRE_PER_MONEY("预存款", Channel.PER_MONEY),
    FBANK("富民富条", Channel.FBANK),
    YIBAO_NEW_FAST_PAY("易宝亿企通快捷支付", Channel.YIBAO_NEW_FAST_PAY),
    YIBAO_NEW_BANK_PAY("易宝亿企通网银支付", Channel.YIBAO_NEW_BANK_PAY),
    FRONT_PAY("店付宝", Channel.FRONT_PAY),//前置购
    VIRTUAL_PAY("虚拟支付", Channel.VIRTUAL_PAY),
    ALLIN_ALIPAY_PAY("通联支付宝扫码支付", Channel.ALLIN_ALIPAY),

    ALLIN_APPLETS_0("通联小程序支付1", Channel.ALLIN_APPLETS_0),
    ALLIN_APPLETS_1("通联小程序支付2", Channel.ALLIN_APPLETS_1),
    BMALL_OFFLINE("线下汇款A", Channel.BMALL_OFFLINE),

    ;

    private String payChannelName;

    private int payChannel;

    public static final TreeMap<Integer, ChannelEnum> INDEX_CHANNEL_ENUM_MAP;
    private static final TreeMap<String, ChannelEnum> content2ChannelEnumMap;

    static {
        INDEX_CHANNEL_ENUM_MAP = new TreeMap<>();
        for (ChannelEnum i : ChannelEnum.values()) {
            INDEX_CHANNEL_ENUM_MAP.put(i.getIndex(), i);
        }
        content2ChannelEnumMap = new TreeMap<>();
        for (ChannelEnum i : ChannelEnum.values()) {
            content2ChannelEnumMap.put(i.getContent(), i);
        }
    }

    public static ChannelEnum getByCont(String content) {
        return content2ChannelEnumMap.getOrDefault(content, null);
    }

    public static ChannelEnum getByIdx(Integer index) {
        return INDEX_CHANNEL_ENUM_MAP.getOrDefault(index, null);
    }

    public static String getPayChannelName(Integer payChannel) {
        return Optional.ofNullable(getByIdx(payChannel))
                .map(ChannelEnum::getContent)
                .orElse("支付渠道不清楚");
    }

    public static TreeMap<Integer, String> getChannelDescMap() {
        return INDEX_CHANNEL_ENUM_MAP.entrySet().stream()
                .filter(e -> Objects.nonNull(e.getValue()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> e.getValue().getContent(),
                        (v1, v2) -> v2,
                        TreeMap::new
                ));
    }

    @Deprecated
    public void setPayChannel(int payChannel) {
        this.payChannel = payChannel;
    }

    @Deprecated
    public void setPayChannelName(String payChannelName) {
        this.payChannelName = payChannelName;
    }

    public String getContent() {
        return getPayChannelName();
    }

    public int getIndex() {
        return getPayChannel();
    }

}